/*
Panoformer Tool 1.0
Command-line tool to convert between different panorama formats


Copyright (c) 2013, Ulf Biallas
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <opencv2/opencv.hpp>
#include "panoformer/Panoformer.hh"

#define PI 3.141592654

#define PANORAMA_EQUIRECTANGULAR 0
#define PANORAMA_CUBIC           1
#define PANORAMA_LITTLEPLANET    2

using namespace std;
using namespace cv;



void printHelp();

int main(int argc, char **argv) {

	int c = 1;
	int input, output, outputWidth, outputHeight;
	bool validInputs = true;
	vector<string> outputPaths;
	vector<string> msgs;


	if(argc<6) {
		validInputs = false;
		msgs.push_back("not enough arguments");
	}

	if(validInputs) {
		string argInputFormat(argv[c++]);
		if(argInputFormat.compare("equirectangular") == 0) {
			input = PANORAMA_EQUIRECTANGULAR;
		} else if(argInputFormat.compare("cubic") == 0) {
			input = PANORAMA_CUBIC;
		} else if(argInputFormat.compare("littleplanet") == 0) {
			input = PANORAMA_LITTLEPLANET;
		} else {
			validInputs = false;
			msgs.push_back("first and second argument have to be 'equirectangular', 'cubic' or 'littleplanet'!");
		}
	}

	if(validInputs) {
		string argOutputFormat(argv[c++]);
		if(argOutputFormat.compare("equirectangular") == 0) {
			output = PANORAMA_EQUIRECTANGULAR;
		} else if(argOutputFormat.compare("cubic") == 0) {
			output = PANORAMA_CUBIC;
		} else if(argOutputFormat.compare("littleplanet") == 0) {
			output = PANORAMA_LITTLEPLANET;
		} else {
			validInputs = false;
			msgs.push_back("first and second argument have to be 'equirectangular', 'cubic' or 'littleplanet'!");
		}
	}

	if(validInputs) {
		if(input == output) {
			validInputs = false;
			msgs.push_back("input and output format have to be different!");
		}
	}

	if(validInputs) {
		string argOutputWidth(argv[c++]);
		outputWidth = atoi(argOutputWidth.c_str());
		if(!(outputWidth > 0 && outputWidth < 65536)) {
			validInputs = false;
			msgs.push_back("third argument has to be a positive integer!");
		}
	}

	if(validInputs) {
		string argOutputHeight(argv[c++]);
		outputHeight = atoi(argOutputHeight.c_str());
		if(!(outputHeight > 0 && outputHeight < 65536)) {
			validInputs = false;
			msgs.push_back("fourth argument has to be a positive integer!");
		}
		if(output == PANORAMA_CUBIC) {
			outputHeight = outputWidth;
		}
	}

	if(validInputs) {
		if(input == PANORAMA_CUBIC || output == PANORAMA_CUBIC) {
			if(argc < c + 7) {
				validInputs = false;
				msgs.push_back("not enough arguments!");
			}
		}
	}

	Mat imgPanoEquirectangular;
	vector<Mat> imagesPanoCubic;
	Mat imgPanoLittleplanet;
	string filepath;

	if(validInputs) {
		switch(input) {
			case PANORAMA_EQUIRECTANGULAR:
				filepath = argv[c++];
				imgPanoEquirectangular = imread(filepath, CV_LOAD_IMAGE_COLOR);
				break;
			case PANORAMA_LITTLEPLANET:
				filepath = argv[c++];
				imgPanoLittleplanet = imread(filepath, CV_LOAD_IMAGE_COLOR);
				break;
			case PANORAMA_CUBIC:
				for(int k=0; k<6; ++k) {
					filepath = argv[c++];
					imagesPanoCubic.push_back(imread(filepath, CV_LOAD_IMAGE_COLOR));
				}
				break;
		}
	}


	if(validInputs) {
		switch(output) {
			case PANORAMA_EQUIRECTANGULAR:
				filepath = argv[c++];
				outputPaths.push_back(filepath);
				imgPanoEquirectangular = Mat(outputHeight, outputWidth, CV_8UC3);
				break;
			case PANORAMA_LITTLEPLANET:
				filepath = argv[c++];
				outputPaths.push_back(filepath);
				imgPanoLittleplanet = Mat(outputHeight, outputWidth, CV_8UC3);
				break;
			case PANORAMA_CUBIC:
				for(int k=0; k<6; ++k) {
					filepath = argv[c++];
					outputPaths.push_back(filepath);
					imagesPanoCubic.push_back(Mat(outputHeight, outputWidth, CV_8UC3));
				}
				break;
		}
	}

	if(validInputs) {
		bool error = false;
		switch(input) {
			case PANORAMA_EQUIRECTANGULAR:
				if(imgPanoEquirectangular.empty()) error = true;
				break;
			case PANORAMA_LITTLEPLANET:
				if(imgPanoLittleplanet.empty()) error = true;
				break;
			case PANORAMA_CUBIC:
				for(int k=0; k<imagesPanoCubic.size(); ++k) if(imagesPanoCubic[k].empty()) error = true;
				break;
		}
		if(error) {
			validInputs = false;
			msgs.push_back("one or more files could not be opened!");
		}
	}



	if(validInputs) {

		Panoformer panoformer;

		switch(input) {
			case PANORAMA_EQUIRECTANGULAR:
				if(output == PANORAMA_LITTLEPLANET) panoformer.equirectangularToLittleplanet(imgPanoEquirectangular, imgPanoLittleplanet);
				if(output == PANORAMA_CUBIC) panoformer.equirectangularToCubic(imgPanoEquirectangular, imagesPanoCubic);
				break;

			case PANORAMA_LITTLEPLANET:
				if(output == PANORAMA_EQUIRECTANGULAR) panoformer.littleplanetToEquirectangular(imgPanoLittleplanet, imgPanoEquirectangular);
				if(output == PANORAMA_CUBIC) panoformer.littleplanetToCubic(imgPanoLittleplanet, imagesPanoCubic);
				break;

			case PANORAMA_CUBIC:
				if(output == PANORAMA_EQUIRECTANGULAR) panoformer.cubicToEquirectangular(imagesPanoCubic, imgPanoEquirectangular);
				if(output == PANORAMA_LITTLEPLANET) panoformer.cubicToLittleplanet(imagesPanoCubic, imgPanoLittleplanet);
				break;
		}

		switch(output) {
			case PANORAMA_EQUIRECTANGULAR:
				imwrite(outputPaths[0], imgPanoEquirectangular);
				break;
			case PANORAMA_LITTLEPLANET:
				imwrite(outputPaths[0], imgPanoLittleplanet);
				break;
			case PANORAMA_CUBIC:
				for(int k=0; k<imagesPanoCubic.size(); ++k) imwrite(outputPaths[k], imagesPanoCubic[k]);
				break;
		}

	}



	if(argc == 1) {
		printHelp();
	} else {
		for(int k=0; k<msgs.size(); ++k) cout << "Error: " << msgs[k] << "\n";
	}

	return 0;
}



void printHelp() {
	cout << "Welcome to the Panoformer tool! \n\n";
	cout << "Call the program with the following arguments: \n";
	cout << "{input format} {output format} {width} {height} {input file(s)} {output file(s)}}\n\n";
	cout << "where {input/output format} is 'equirectangular', 'cubic' or 'littleplanet'\n";
	cout << "and {width/height} the size of the output image(s).\n";
	cout << "Equirectangular and Littleplanet panoramas consist of one file, cubic panoramas consist of 6 files in the order px/nx/py/ny/pz/nz.\n\n";
	cout << "Example:\n";
	cout << "panoconverter equirectangular cubic 200 200 panoequi.png xp.png xn.png yp.png yn.png zp.png zn.png\n";
}